/*
 * JsonArray.h
 *
 *  Created on: 2021-09-15
 *      Author: chuanjiang.zh
 */

#ifndef JSONARRAY_H_
#define JSONARRAY_H_

#include "BasicType.h"
#include "json/json.h"
#include <vector>
#include "TStringCast.h"


class JsonArray
{
public:
    JsonArray();
    virtual ~JsonArray();

    template < class SequenceCont >
    static void toJson(const SequenceCont& seq, Json::Value& value)
    {
        typename SequenceCont::const_iterator pos = seq.begin();
        for (; pos != seq.end(); ++pos)
        {
            value.append(*pos);
        }
    }

    template < class SequenceCont >
    static bool parseString(const Json::Value& value, SequenceCont& seq)
    {
        if (!value.isArray())
        {
            return false;
        }

        try
        {
            for (Json::ArrayIndex i = 0; i < value.size(); i ++)
            {
                Json::ValueType t = value[i].type();
                if (t == Json::nullValue)
                {
                    seq.push_back(std::string());
                }
                else if (t == Json::intValue)
                {
                    seq.push_back(comn::StringCast::toString(value[i].asInt64()));
                }
                else if (t == Json::uintValue)
                {
                    seq.push_back(comn::StringCast::toString(value[i].asUInt64()));
                }
                else if (t == Json::realValue)
                {
                    seq.push_back(comn::StringCast::toString(value[i].asDouble()));
                }
                else if (t == Json::booleanValue)
                {
                    seq.push_back(comn::StringCast::toString(value[i].asBool()));
                }
                else if (t == Json::stringValue)
                {
                    seq.push_back(value[i].asString());
                }
                else
                {
                    seq.push_back(std::string());
                }
            }
        }
        catch (std::exception& ex)
        {
			ex.what();
            return false;
        }

        return true;
    }

    template < class SequenceCont >
    static bool parseNumber(const Json::Value& value, SequenceCont& seq)
    {
        if (!value.isArray())
        {
            return false;
        }

        try
        {
            for (Json::ArrayIndex i = 0; i < value.size(); i ++)
            {
                Json::ValueType t = value[i].type();
                if (t == Json::nullValue)
                {
                    seq.push_back(0);
                }
                else if (t == Json::intValue)
                {
                    seq.push_back(value[i].asInt64());
                }
                else if (t == Json::uintValue)
                {
                    seq.push_back(value[i].asUInt64());
                }
                else if (t == Json::realValue)
                {
                    seq.push_back(value[i].asDouble());
                }
                else if (t == Json::booleanValue)
                {
                    seq.push_back(value[i].asBool());
                }
                else
                {
                    seq.push_back(0);
                }
            }
        }
        catch (std::exception& ex)
        {
            ex.what();
            return false;
        }

        return true;
    }



};

#endif /* JSONARRAY_H_ */
